{% extends 'base/base.html' %}

{% block title %}{{ title }} - 多用户报警系统{% endblock %}

{% block content %}
<div class="container main-container">
    <div class="row mb-4">
        <div class="col-lg-12">
            <h1 class="page-title">{{ title }}</h1>
        </div>
    </div>
    
    <div class="row mb-4">
        <div class="col-lg-8 offset-lg-2">
            <div class="card">
                <div class="card-header">
                    <h3 class="card-title">{{ title }}</h3>
                </div>
                
                <div class="card-body">
                    {% if messages %}
                        {% for message in messages %}
                            <div class="alert alert-{{ message.tags }} alert-dismissible fade show" role="alert">
                                {{ message }}
                                <button type="button" class="close" data-dismiss="alert" aria-label="Close">
                                    <span aria-hidden="true">&times;</span>
                                </button>
                            </div>
                        {% endfor %}
                    {% endif %}
                    
                    <form method="post">
                        {% csrf_token %}
                        
                        <div class="form-group">
                            <label for="id_name">{{ form.name.label }} <span class="text-danger">*</span></label>
                            {{ form.name }}
                            {% if form.name.errors %}
                                <div class="invalid-feedback d-block">
                                    {{ form.name.errors.0 }}
                                </div>
                            {% endif %}
                            <small class="form-text text-muted">请输入定时任务的名称，用于标识不同的任务</small>
                        </div>
                        
                        <div class="form-group">
                            <label for="id_cron_expression">{{ form.cron_expression.label }} <span class="text-danger">*</span></label>
                            {{ form.cron_expression }}
                            {% if form.cron_expression.errors %}
                                <div class="invalid-feedback d-block">
                                    {{ form.cron_expression.errors.0 }}
                                </div>
                            {% endif %}
                            <small class="form-text text-muted">请输入Cron表达式，格式为：分钟 小时 日 月 星期，例如：0 12 * * * 表示每天12点执行</small>
                        </div>
                        
                        <div class="form-group">
                            <label for="id_is_enabled">{{ form.is_enabled.label }}</label>
                            <div class="form-check">
                                {{ form.is_enabled }}
                                <label class="form-check-label" for="id_is_enabled">
                                    {{ form.is_enabled.label }}
                                </label>
                            </div>
                            <small class="form-text text-muted">启用后，系统将按照设定的Cron表达式定时执行该任务</small>
                        </div>
                        
                        <div class="form-group">
                            <label for="id_description">{{ form.description.label }}</label>
                            {{ form.description }}
                            {% if form.description.errors %}
                                <div class="invalid-feedback d-block">
                                    {{ form.description.errors.0 }}
                                </div>
                            {% endif %}
                            <small class="form-text text-muted">可选，输入该定时任务的描述信息，帮助理解任务的用途</small>
                        </div>
                        
                        <div class="form-group">
                            <label for="id_project_filter">{{ form.project_filter.label }}</label>
                            {{ form.project_filter }}
                            {% if form.project_filter.errors %}
                                <div class="invalid-feedback d-block">
                                    {{ form.project_filter.errors.0 }}
                                </div>
                            {% endif %}
                            <small class="form-text text-muted">可选，为空时将对所有项目执行任务</small>
                        </div>
                        
                        <div class="form-group">
                            <label for="id_webhook_timeout">{{ form.webhook_timeout.label }}</label>
                            {{ form.webhook_timeout }}
                            {% if form.webhook_timeout.errors %}
                                <div class="invalid-feedback d-block">
                                    {{ form.webhook_timeout.errors.0 }}
                                </div>
                            {% endif %}
                            <small class="form-text text-muted">Webhook请求的超时时间（秒），默认为系统配置的WEBHOOK_TIMEOUT值</small>
                        </div>
                        
                        <div class="form-group">
                            <label for="id_retry_times">{{ form.retry_times.label }}</label>
                            {{ form.retry_times }}
                            {% if form.retry_times.errors %}
                                <div class="invalid-feedback d-block">
                                    {{ form.retry_times.errors.0 }}
                                </div>
                            {% endif %}
                            <small class="form-text text-muted">任务执行失败时的重试次数，默认为3次</small>
                        </div>
                        
                        <div class="form-group">
                            <label for="id_retry_interval">{{ form.retry_interval.label }}</label>
                            {{ form.retry_interval }}
                            {% if form.retry_interval.errors %}
                                <div class="invalid-feedback d-block">
                                    {{ form.retry_interval.errors.0 }}
                                </div>
                            {% endif %}
                            <small class="form-text text-muted">重试间隔时间（秒），默认为5秒</small>
                        </div>
                        
                        <div class="form-group">
                            <label for="id_last_executed_at">最后执行时间</label>
                            <input type="text" class="form-control" value="{{ task.last_executed_at|date:'Y-m-d H:i:s' }}" readonly>
                        </div>
                        
                        <div class="form-group">
                            <label for="id_last_execution_status">最后执行状态</label>
                            <input type="text" class="form-control" value="{% if task.last_execution_status %}成功{% else %}{% if task.last_executed_at %}失败{% else %}从未执行{% endif %}{% endif %}" readonly>
                        </div>
                        
                        <div class="form-group">
                            <label for="id_last_execution_log">最后执行日志</label>
                            <textarea class="form-control" rows="3" readonly>{{ task.last_execution_log }}</textarea>
                        </div>
                        
                        <div class="form-group">
                            <label for="id_created_at">创建时间</label>
                            <input type="text" class="form-control" value="{{ task.created_at|date:'Y-m-d H:i:s' }}" readonly>
                        </div>
                        
                        <div class="form-group">
                            <label for="id_updated_at">更新时间</label>
                            <input type="text" class="form-control" value="{{ task.updated_at|date:'Y-m-d H:i:s' }}" readonly>
                        </div>
                        
                        <div class="form-group">
                            <label for="id_created_by">创建人</label>
                            <input type="text" class="form-control" value="{{ task.created_by.username }}" readonly>
                        </div>
                        
                        <div class="form-group">
                            <label for="id_updated_by">更新人</label>
                            <input type="text" class="form-control" value="{{ task.updated_by.username }}" readonly>
                        </div>
                        
                        <div class="form-group text-center">
                            <button type="submit" class="btn btn-primary">
                                <i class="fa fa-check"></i> 保存
                            </button>
                            <a href="{% url 'tasks:scheduled_task_list' %}" class="btn btn-secondary ml-2">
                                <i class="fa fa-times"></i> 取消
                            </a>
                        </div>
                    </form>
                </div>
            </div>
        </div>
    </div>
</div>
{% endblock %}